from datetime import datetime,timedelta
from pymongo import MongoClient,errors
from bs4 import BeautifulSoup
from pymongo import MongoClient
import requests
import urllib.parse
import urllib.request
import urllib.robotparser
import math
import datetime,json,random,socket,csv,time, re,queue,sys

class CrawlerQueue:
    
    OUTSTANDING,PROCESSING,COMPLETE,UNUSEFULL=range(4)

    def __init__(self,collection=None,status='status',popfield='_id',timeout=300):
        # self.client=MongoClient('localhost',27017) if client is None else client
        # self.db=self.client.cache
        self.popfield=popfield
        self.status=status
        self.collection=collection
        self.timeout=timeout
    
    def __nonzero__(self):
        record=self.collection.find_one(
            {self.status:{'$ne':self.COMPLETE}}
        )
        return True if record else False

    # def push(self,id):

    #     try:
    #         self.db.pro

    def pop(self):

        record=self.collection.find_and_modify(
            query={self.status:self.OUTSTANDING},
            update={'$set':{self.status:self.PROCESSING}}
        )
        # record=self.collection.find_one()
        if record:
            return record
        else:
            self.repair()
            return False            
            # raise KeyError()
    
    def peek(self):
        record=self.collection.find_one({self.status:self.OUTSTANDING})
        if record:
            return record
    
    def complete(self,id):
        #print('yes:',id)
        self.collection.update({self.popfield:id},{'$set':{self.status:self.COMPLETE}})

    def uncomplete(self,id):
        #print('no:',id)
        self.collection.update({self.popfield:id},{'$set':{self.status:self.OUTSTANDING}})
    def remove(self,id):
        self.collection.update({self.popfield:id},{'$set':{self.status:self.UNUSEFULL}})
    def delqueue(self,id):
        self.collection.remove({self.popfield:id})


    def repair(self):
        record = self.collection.find_and_modify(
            query={                
                self.status: {'$ne': self.COMPLETE}
            },
            update={'$set': {self.status: self.OUTSTANDING}}
        )
        if record:
            print('Released:', record[self.popfield])